import { createStore } from "vuex";
import router from "../router";

export default createStore({
  state: {
    token:
      localStorage.getItem("token") || sessionStorage.getItem("token") || "",
  },
  mutations: {
    //根据remember来设置token保存在浏览器中
    setToken(state, { token, remember }) {
      state.token = token;
      remember
        ? localStorage.setItem("token", token)
        : sessionStorage.setItem("token", token);
      router.push({ name: "Index" });
    },
    logout(state) {
      //清空浏览器token缓存，并返回登录页
      state.token = "";
      localStorage.removeItem("token");
      sessionStorage.removeItem("token");
      router.push({ name: "Login" });
    },
  },
  actions: {},
  modules: {},
});
